<template>
  <div class="register">
    <div class="pic-box">
      <div class="inner-pic">
        <progressive-img :placeholder="bigPic" :origin="bigPic" />
      </div>
      <div class="pic-bg">
        <progressive-img :placeholder="smallPic" :origin="smallPic" />
      </div>
    </div>
    <div class="right-box">
      <el-form ref="registerRef" :model="registerForm" :rules="registerRules" class="register-form">
        <img class="logo" src="https://yheyan.oss-cn-beijing.aliyuncs.com/images/LOGO.jpg" alt="">
        <el-form-item prop="company_name">
          <el-input v-model="registerForm.company_name" type="text" size="large" auto-complete="off" placeholder="用户名">
            <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input v-model="registerForm.mobile" type="text" size="large" auto-complete="off" placeholder="手机号">
            <template #prefix><svg-icon icon-class="phone" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input v-model="registerForm.pass" type="password" size="large" auto-complete="off" placeholder="密码"
            @keyup.enter="handleRegister">
            <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPass">
          <el-input v-model="registerForm.confirmPass" type="password" size="large" auto-complete="off"
            placeholder="确认密码" @keyup.enter="handleRegister">
            <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="smscode">
          <el-input v-model="registerForm.smscode" type="text" size="large" auto-complete="off" placeholder="验证码"
            @keyup.enter="handleRegister">
            <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
            <template #append>
              <el-button type="primary" style="color: #ffffff" :disabled="isSendCode"
                :style="{ background: isSendCode ? '#2b2f3a50' : '#2b2f3a' }" size="large" @click="sendSmsCode">{{
                  !isSendCode ? '发送验证码' : sendTime + '秒后重试' }}</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleRegister">
            <span v-if="!loading">注 册</span>
            <span v-else>注 册 中...</span>
          </el-button>
          <div style="float: right;">
            <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from "element-plus"
import { getCode, register } from "@/api/login"
import ProgressiveImg from "@/components/ProgressiveImg/index"

const router = useRouter()
const { proxy } = getCurrentInstance()

const registerForm = ref({
  company_name: null,
  mobile: null,
  pass: null,
  comfirmPass: null,
  smscode: null
});

const equalToPassword = (rule, value, callback) => {
  if (registerForm.value.password !== value) {
    callback(new Error("两次输入的密码不一致"))
  } else {
    callback()
  }
}

const smallPic = ref('https://yheyan.oss-cn-beijing.aliyuncs.com/images/login_bg_icon.png')
const bigPic = ref('https://yheyan.oss-cn-beijing.aliyuncs.com/images/login_bg_pic.png')

const registerRules = {
  company_name: [
    { required: true, trigger: "blur", message: "请输入您的账号" },
    { min: 2, max: 20, message: "用户账号长度必须介于 2 和 20 之间", trigger: "blur" }
  ],
  mobile: [
    { required: true, trigger: "blur", message: "请输入您的手机号" },
    { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "手机号格式不正确", trigger: "blur" }
  ],
  password: [
    { required: true, trigger: "blur", message: "请输入您的密码" },
    { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
    { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
  ],
  comfirmPass: [
    { required: true, trigger: "blur", message: "请再次输入您的密码" },
    { required: true, validator: equalToPassword, trigger: "blur" }
  ],
  smscode: [{ required: true, trigger: "change", message: "请输入验证码" }]
}

const loading = ref(false)
const captchaEnabled = ref(true)

const isSendCode = ref(false)
const sendTime = ref(60)
const timer = ref(null)

function handleRegister() {
  proxy.$refs.registerRef.validate(valid => {
    if (valid) {
      loading.value = true;
      register(registerForm.value).then(res => {
        const company_name = registerForm.value.company_name;
        ElMessageBox.alert("<font color='red'>恭喜你，您的账号 " + company_name + " 注册成功！</font>", "系统提示", {
          dangerouslyUseHTMLString: true,
          type: "success",
        }).then(() => {
          router.push("/login");
        }).catch(() => { });
      }).catch(() => {
        loading.value = false;
        if (captchaEnabled) {
          getCode();
        }
      })
    }
  })
}

// 发送验证码
const sendSmsCode = () => {
  if (!registerForm.value.mobile) return proxy.$message.error('请输入手机号')
  const data = {
    mobile: registerForm.value.mobile
  }

  getCode(data).then(res => {
    isSendCode.value = true

    timer.value = setInterval(() => {
      sendTime.value--
      if (sendTime.value <= 0) {
        clearInterval(timer.value)
        isSendCode.value = false
        sendTime.value = 60
      }
    }, 1000)
  })
}
</script>

<style lang='scss' scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: #ffffff;

  .pic-box {
    width: 590px;
    height: 100%;
    position: relative;

    .inner-pic {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 10;
    }
    .pic-bg {
      position: absolute;
      top: 50%;
      left: 50%;
      z-index: 100;
      width: 428px;
      height: 428px;
      transform: translate(-50%, -50%);
    }
  }

  .right-box {
    width: calc(100% - 590px);
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;

    .logo {
      width: 160px;
      height: 120px;
      margin-bottom: 16px;
    }
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  background: rgba(255, 255, 255, 0.8);
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.3);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  text-align: center;

  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}

.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.register-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.register-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
